package com.sx.zongzhi.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sx.zongzhi.R;
import com.sx.zongzhi.activity.ImagePlayActivity;
import com.sx.zongzhi.activity.VideoPlayerActivity;
import com.sx.zongzhi.bean.PhotoBean;
import com.sx.zongzhi.net.ServiceFactory;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.widget.AudioDialog;

import java.io.File;
import java.util.List;


/**
 * @desc:
 */
public class PictureAdapter extends BaseQuickAdapter<PhotoBean, BaseViewHolder> {

   private Context context;

    public TextView tv_num;
    private List<PhotoBean> data;
    private AudioDialog audioDialog;

    public PictureAdapter(Context context, @Nullable List<PhotoBean> data) {
        super(R.layout.item_rvpicture, data);
        this.context=context;
        this.data=data;
    }


    @Override
    protected void convert(final BaseViewHolder viewHolder, final PhotoBean item) {
        ImageView imageView =  viewHolder.getView(R.id.iv_picture);
        ImageView tv_duration = viewHolder.getView(R.id.tv_duration);
        tv_num = viewHolder.getView(R.id.tv_num);
        LinearLayout ll_bottom = viewHolder.getView(R.id.ll_bottom);
        String mimeType=item.getSuffixType();
        final String path = ServiceFactory.NEW_API_BASE_URL + File.separator +item.getPath();
        if (".wav".equalsIgnoreCase(mimeType) || ".amr".equalsIgnoreCase(mimeType)) {//音频
            ll_bottom.setVisibility(View.VISIBLE);
            tv_num.setVisibility(View.GONE);
            tv_duration.setImageResource(R.mipmap.icon_audios);
            if (audioDialog==null){
                audioDialog=new AudioDialog(context);
                audioDialog.setCancelable(false);
            }
            imageView.setImageResource(R.mipmap.img_audio);
        } else if(".mp4".equalsIgnoreCase(mimeType)){//视频
            ll_bottom.setVisibility(View.VISIBLE);
            tv_num.setVisibility(View.GONE);
            tv_duration.setImageResource(R.drawable.video_icon);
            imageView.setImageResource(R.mipmap.img_video);
        }else if (".png".equalsIgnoreCase(mimeType)||".jpeg".equalsIgnoreCase(mimeType)||
                ".jpg".equalsIgnoreCase(mimeType)){//图片
            ll_bottom.setVisibility(View.GONE);
            RequestOptions options = new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.img_fail)
                    .error(R.mipmap.img_fail)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE);
            Glide.with(context)
                    .load(path)
                    .apply(options)
                    .into(imageView);
        }else {
            ll_bottom.setVisibility(View.GONE);
            imageView.setImageResource(R.mipmap.img_file);
        }
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String type = item.getSuffixType();
                if(".mp4".equalsIgnoreCase(type)){
                    context.startActivity(new Intent(context,
                            VideoPlayerActivity.class).putExtra(
                            "path",path));
                }else if(".wav".equalsIgnoreCase(type) || ".amr".equalsIgnoreCase(type)){
                    if (audioDialog!=null){
                        audioDialog.show(audioDialog.AUDIO_PLAY);
                        audioDialog.startPlay(path);
                    }
//                    if (player == null){
//                        player = new Player(context);
//                    }
//                    if (item.isPlay){
//                        player.stop();
//                        item.isPlay = false;
//                        notifyDataSetChanged();
//                    }else {
//                        for(int i = 0 ; i <data.size() ; i++){
//                            if (data.get(i).isPlay){
//                                player.stop();
//                                data.get(i).isPlay = false;
//                            }
//                        }
//                        item.isPlay=true;
//                        notifyDataSetChanged();
//                        player.playUrl(path,null, new Player.OnCompletionListener() {
//                            @Override
//                            public void onPlayComplet() {
//                                item.isPlay = false;
//                                notifyDataSetChanged();
//                            }
//                        });
//
//                    }

                }else if (".jpeg".equalsIgnoreCase(type)||".png".equalsIgnoreCase(type)||
                        ".jpg".equalsIgnoreCase(type)){
                    context.startActivity(new Intent(context, ImagePlayActivity.class).putExtra(
                            "path", path));
                }else {
                    ToastUtils.showToast(context,"不能查看文件");
                }
            }
        });

    }
}
